<?
namespace Home\Model;
use Think\Model;

class RatingModel extends Model {

	//根据项目ID获取该项目要填报的调查表数据
	//item_id
	public function rateinfo($item_id){
		$return=array();
		$iteminfo=M("Iteminfo")->where("item_id=".$item_id)->find();
		if($iteminfo){
			//根据项目行业信息获取评测大分类
			$topcate=M("Topcate")->where("indus_id=".$iteminfo['indus_id'])->order("top_id asc")->select();
			foreach($topcate as $k=>$v){
				//根据大分类获取二级分类信息
				$subcate=M("Subcate")->where("top_id=".$v['top_id'])->order("sub_id asc")->select();
				foreach($subcate as $key=>$value){
					//根据二级分类获取三级回答分类信息
					$subcate[$key]['threecate']=M("Threecate")->where("sub_id=".$value['sub_id'])->order("three_id asc")->select();
				}
				$topcate[$k]['subcate']=$subcate;
			}
			return $topcate;
		}else{
			return $return;
		}
	}

	//企业项目提交==============================================
	//处理评测信息提交上来的数据
	public function arrange($item_id,$info){
		//查看项目行业是否启用
		$iteminfo=M("Iteminfo")->join("left join ec_industry on ec_iteminfo.indus_id=ec_industry.indus_id")->where("item_id=".$item_id)->field("ec_iteminfo.*,ec_industry.indus_isopen,ec_industry.indus_score,ec_industry.indus_weight")->find();
		//行业开启关闭状态
		$isopen=false;
		if($iteminfo['indus_isopen']==1){//1为启用，2为测试
			$isopen=true;
		}elseif($iteminfo['indus_isopen']==2){
			$isopen=false;
		}
		//评测信息入库
		$rating=array();
		foreach($info as $k=>$v){
			$subcate=M("Subcate")->where("sub_id=".$k)->find();
			//根据key(二级ID)获取对应三级分类信息
			$threecate=M("Threecate")->where("sub_id=".$k)->order("three_id asc")->select();
			//0,1代表选中与否，或者输入的数值
			if($subcate['sub_type']==1){//单选
				foreach($threecate as $key=>$value){
					if($v==$value['three_id']){//选中id=三级分类ID
						$rating[]=array(
							'item_id'=>$item_id,
							'three_id'=>$value['three_id'],
							'rate_value'=>1,
							'rate_score'=>$isopen?$value['three_score']:0,
						);
					}else{
						$rating[]=array(
							'item_id'=>$item_id,
							'three_id'=>$value['three_id'],
							'rate_value'=>0,
							'rate_score'=>0,
						);
					}
				}
			}elseif($subcate['sub_type']==2){//多选
				foreach($threecate as $key=>$value){
					if(in_array($value['three_id'],$v)){//三级分类ID在多选选中id中
						$rating[]=array(
							'item_id'=>$item_id,
							'three_id'=>$value['three_id'],
							'rate_value'=>1,
							'rate_score'=>$isopen?$value['three_score']:0,
						);
					}else{
						$rating[]=array(
							'item_id'=>$item_id,
							'three_id'=>$value['three_id'],
							'rate_value'=>0,
							'rate_score'=>0,
						);
					}
				}
			}elseif($subcate['sub_type']==3){//数字填写
				foreach($threecate as $key=>$value){
					$rate_score=0;
					if($v[$value['three_id']]>$subcate['sub_max']){
						$rate_score=$subcate['sub_max']*$value['three_score'];
					}else{
						$rate_score=$v[$value['three_id']]*$value['three_score'];
					}
					$rating[]=array(
						'item_id'=>$item_id,
						'three_id'=>$value['three_id'],
						'rate_value'=>$v[$value['three_id']],
						'rate_score'=>$isopen?$rate_score:0,
					);
				}
			}elseif($subcate['sub_type']==4){//描述填写
				foreach($threecate as $key=>$value){
					$rate_score=0;
					if($isopen){
						$rate_score=$this->keywordscore($v[$value['three_id']],$item_id);//根据关键词出现频率计算分值，下面处理
					}else{
						$rate_score=0;
					}					
					$rating[]=array(
						'item_id'=>$item_id,
						'three_id'=>$value['three_id'],
						'rate_value'=>$v[$value['three_id']],
						'rate_score'=>$rate_score,
					);
				}
			}
			unset($subcate);unset($threecate);
		}
		//rating入库
		$this->where("item_id=".$item_id)->delete();//插入前删除
		$this->addAll($rating);
		//行业开启关闭状态
		if($isopen){//为启用
			//$rating入库，并通过各个分值权重计算总分
			$this->getscore($item_id,$info);
		}else{//为测试
			//$rating入库即可
		}
		return true;
	}
	//通过各个分值权重计算总分
	public function getscore($item_id,$info){
		$score=0;
		//$rating入库，并通过各个分值权重计算总分
		foreach($info as $k=>$v){
			$subcate=M("Subcate")->where("sub_id=".$k)->find();
			//根据key(二级ID)获取对应三级分类信息
			$threecate=M("Threecate")->where("sub_id=".$k)->order("three_id asc")->field("three_id")->select();
			$threecate_=array();
			foreach($threecate as $key=>$value){
				$threecate_[]=$value['three_id'];
			}
			//三级分类各项分值
			$ratesum=$this->where("three_id in(".join(",",$threecate_).")")->sum('rate_score');
			//$score+=round($ratesum*$subcate['sub_weight']/100,2);
			$score+=round($ratesum,2);
			unset($subcate);unset($threecate);unset($ratesum);
		}
		//行业分值权重
		//$score+=round($iteminfo['indus_score']*$iteminfo['indus_weight']/100,2);
		$score+=round($iteminfo['indus_score'],2);
		//更新分值到项目表中
		M("Iteminfo")->where("item_id=".$item_id)->save(array("score"=>$score));
		return true;
	}
	//===================================


	//后台手动给项目各项打分==============================================
	public function ratescore($item_id,$rate){
		//各项分值入库
		foreach($rate as $k=>$v){
			$this->where("rate_id=".$k)->save(array("rate_score"=>$v));
		}
		//
		$iteminfo=M("Iteminfo")->where("item_id=".$item_id)->find();
		//根据项目行业信息获取评测大分类
		$topcate=M("Topcate")->where("indus_id=".$iteminfo['indus_id'])->order("top_id asc")->select();
		$subcate=array();
		foreach($topcate as $k=>$v){
			//根据大分类获取二级分类信息
			$subcate_=M("Subcate")->where("top_id=".$v['top_id'])->order("sub_id asc")->select();
			$subcate=array_merge($subcate_, $subcate);
		}
		//计算分值
		$score=0;
		foreach($subcate as $k=>$v){
			//根据key(二级ID)获取对应三级分类信息
			$threecate=M("Threecate")->where("sub_id=".$v['sub_id'])->order("three_id asc")->field("three_id")->select();
			if($threecate){
				$threecate_=array();
				foreach($threecate as $key=>$value){
					$threecate_[]=$value['three_id'];
				}
				//三级分类各项分值
				$ratesum=$this->where("three_id in(".join(",",$threecate_).")")->sum('rate_score');
				//$score+=round($ratesum*$subcate['sub_weight']/100,2);
				$score+=round($ratesum,2);
				unset($ratesum);
			}
			unset($subcate);unset($threecate);
		}
		//行业分值权重
		//$score+=round($iteminfo['indus_score']*$iteminfo['indus_weight']/100,2);
		$score+=round($iteminfo['indus_score'],2);
		//更新分值到项目表中
		M("Iteminfo")->where("item_id=".$item_id)->save(array("score"=>$score));
		return true;
	}
	//===================================

	//@remarks 描述简介
	//@item_id 项目id
	public function keywordscore($remarks,$item_id){
		return 20;
	}
}